package com.smart.campus.web.controller.school;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.school.domain.AdjustCourseAdmin;
import com.smart.campus.school.service.IAdjustCourseAdminService;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.common.core.page.TableDataInfo;

/**
 * 调课管理员设置Controller
 * 
 * @author Zhihe.Li
 * @date 2020-11-03
 */
@Controller
@RequestMapping("/school/adjustCourseAdmin")
public class AdjustCourseAdminController extends BaseController
{
    private String prefix = "school/adjustCourseAdmin";

    @Autowired
    private IAdjustCourseAdminService adjustCourseAdminService;

    @RequiresPermissions("school:adjustCourseAdmin:view")
    @GetMapping()
    public String adjustCourseAdmin()
    {
        return prefix + "/adjustCourseAdmin";
    }

    /**
     * 查询调课管理员设置列表
     */
    @RequiresPermissions("school:adjustCourseAdmin:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AdjustCourseAdmin adjustCourseAdmin)
    {
        startPage();
        List<AdjustCourseAdmin> list = adjustCourseAdminService.selectAdjustCourseAdminList(adjustCourseAdmin);
        return getDataTable(list);
    }

    /**
     * 导出调课管理员设置列表
     */
    @RequiresPermissions("school:adjustCourseAdmin:export")
    @Log(title = "调课管理员设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AdjustCourseAdmin adjustCourseAdmin)
    {
        List<AdjustCourseAdmin> list = adjustCourseAdminService.selectAdjustCourseAdminList(adjustCourseAdmin);
        ExcelUtil<AdjustCourseAdmin> util = new ExcelUtil<AdjustCourseAdmin>(AdjustCourseAdmin.class);
        return util.exportExcel(list, "adjustCourseAdmin");
    }

    /**
     * 新增调课管理员设置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存调课管理员设置
     */
    @RequiresPermissions("school:adjustCourseAdmin:add")
    @Log(title = "调课管理员设置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AdjustCourseAdmin adjustCourseAdmin)
    {
        AdjustCourseAdmin query = new AdjustCourseAdmin();
        query.setGradeId(adjustCourseAdmin.getGradeId());
        List<AdjustCourseAdmin> list = adjustCourseAdminService.selectAdjustCourseAdminList(query);
        if(CollectionUtils.isNotEmpty(list)){
            return AjaxResult.error("该年级已经设置过！");
        }
        return toAjax(adjustCourseAdminService.insertAdjustCourseAdmin(adjustCourseAdmin));
    }

    /**
     * 修改调课管理员设置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AdjustCourseAdmin adjustCourseAdmin = adjustCourseAdminService.selectAdjustCourseAdminById(id);
        mmap.put("adjustCourseAdmin", adjustCourseAdmin);
        return prefix + "/edit";
    }

    /**
     * 修改保存调课管理员设置
     */
    @RequiresPermissions("school:adjustCourseAdmin:edit")
    @Log(title = "调课管理员设置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AdjustCourseAdmin adjustCourseAdmin)
    {
        return toAjax(adjustCourseAdminService.updateAdjustCourseAdmin(adjustCourseAdmin));
    }

    /**
     * 删除调课管理员设置
     */
    @RequiresPermissions("school:adjustCourseAdmin:remove")
    @Log(title = "调课管理员设置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(adjustCourseAdminService.deleteAdjustCourseAdminByIds(ids));
    }
}
